package com.dianmi.seckill.common.config;

import com.dianmi.seckill.common.concurrent.BiMultiTaskWaitResultService;
import com.dianmi.seckill.common.concurrent.MultiTaskWaitResultService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author xieqiaofu
 * @date 2020/12/3
 */
@Configuration
public class ExecutorConfig {

    @Value("${eebo.multitask.pool.capacity:2000}")
    private int capacity;

    @Value("${eebo.multitask.pool.threadNums:8}")
    private int threadNums;

    @Bean
    public Executor executor() {
        return new ThreadPoolExecutor(threadNums, threadNums, 10000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(capacity), new ThreadPoolExecutor.AbortPolicy());
    }

    @Bean
    public MultiTaskWaitResultService multiTaskWaitResultService() {
        return new MultiTaskWaitResultService(executor());
    }

    @Bean
    public BiMultiTaskWaitResultService biMultiTaskWaitResultService() {
        return new BiMultiTaskWaitResultService(executor());
    }
}
